﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;

namespace StudentManagementSystem
{
    public class Logger
    {
        private string connectionString = "Data Source=StudentManagement.db;Version=3;";

        public void Log(string action, string detail)
        {
            using (SqliteConnection connection = new SqliteConnection(connectionString))
            {
                connection.Open();
                string query = "INSERT INTO Logs (Action, Detail, LogTime) VALUES (@Action, @Detail, @LogTime)";

                using (SqliteCommand command = new SqliteCommand(query, connection))
                {
                    command.Parameters.AddWithValue("@Action", action);
                    command.Parameters.AddWithValue("@Detail", detail);
                    command.Parameters.AddWithValue("@LogTime", DateTime.Now);

                    command.ExecuteNonQuery();
                }
            }
            Console.WriteLine($"Logged: {action} - {detail}");
        }

        public void DisplayLogs()
        {
            using (SqliteConnection connection = new SqliteConnection(connectionString))
            {
                connection.Open();
                string query = "SELECT * FROM Logs";

                using (SqliteCommand command = new SqliteCommand(query, connection))
                {
                    using (SqliteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string action = reader["Action"].ToString();
                            string detail = reader["Detail"].ToString();
                            string logTime = reader["LogTime"].ToString();

                            Console.WriteLine($"LogTime: {logTime}, Action: {action}, Detail: {detail}");
                        }
                    }
                }
            }
        }
    }
}
